package com.abel.bigwater.mapper

import com.abel.bigmeter.service.DataParam
import com.abel.bigmeter.service.DmaParam
import com.abel.bigwater.model.*
import org.apache.ibatis.annotations.Mapper

@Mapper
interface DataMapper {

    fun selectMeterBrand(): List<BwMeterBrand>

    fun selectRemoteBrand(): List<BwRemoteBrand>

    fun insertMeter(meter: BwMeter): Int

    fun deleteMeter(ids: String): Int

    fun updateMeter(meter: BwMeter): Int

    /**
     * <pre>
     * <parameterMap type="map" id="meterWhere">
     * <parameter property="id"></parameter>
     * <parameter property="name"></parameter>
     * <parameter property="dmaId"></parameter>
     * <parameter property="typeId"></parameter>
     * <parameter property="location"></parameter>
     * <parameter property="installDate1"></parameter>
     * <parameter property="installDate2"></parameter>
     * <parameter property="meterBrandId"></parameter>
     * <parameter property="remoteBrandId"></parameter>
     * <parameter property="firmId"></parameter>
    </parameterMap> *
     * typeId can be: CHECK/CHARGE/FIRE
    </pre> *
     *
     * @param map
     * @return
     */
    fun selectMeter(map: Map<String, Any>): List<BwMeter>

    /**
     * The meter where type = 'CHECK'.
     *
     * @return
     */
    fun selectParentMeter(map: Map<String, Any>): List<BwMeter>

    /**
     * The meter with type = 'CHECK' for specified DMA.
     *
     * @param dmaId
     * @return
     */
    fun selectParentMeterByDmaId(dmaId: String): List<BwMeter>

    fun selectDma(map: Map<String, Any>): List<BwDma>

    fun selectDmaById(dp: DmaParam): BwDma?

    fun insertDma(dma: BwDma): Int

    fun updateDma(dma: BwDma): Int

    /**
     * 更改DMA状态, refer to [MeterStatus]
     * @param dp
     * @return
     */
    fun updateDmaStatus(dp: DmaParam): Int

    fun detachDmaUser(userId: String)

    /**
     * Attach the user to the DMA list.
     *
     * @param map - userId, dmaIds - can be like "'DMA01', 'DMA02'".
     */
    fun attachDmaUser(map: Map<String, String>)

    fun deleteDma(ids: String): Int

    /**
     * <pre>
     * <parameterMap type="map" id="linkMeterDmaMap">
     * <parameter property="dmaId"></parameter>
     * <parameter property="meterIds"></parameter>
    </parameterMap> *
    </pre> *
     */
    fun linkMeterDma(map: Map<String, String>)

    fun detachMeterDma(map: Map<String, String>)

    /**
     * ORDER BY sample_time [ASC].
     *
     * <pre>
     * <parameterMap type="map" id="realtimeMap">
     * <parameter property="meterId"></parameter>
     * <parameter property="sampleTime1"></parameter>
     * <parameter property="sampleTime2"></parameter>
     * <parameter property="dmaId"></parameter>
     * <parameter property="index"></parameter>
     * <parameter property="rows"></parameter>
     * <parameter property="firmId"></parameter>
    </parameterMap> *
    </pre> *
     *
     * @param dataParam
     * @return
     */
    fun selectRealtime(dataParam: DataParam): List<BwData>

    /**
     * select last 1000 rows from bw_realtime_data and join with other tables.
     *
     * @param map
     * @return
     */
    fun selectRealtimeLast(map: Map<String, Any>): List<BwData>

    /**
     * This method will retrieve realtime data hourly.
     *
     * @param dataParam
     * @return
     */
    fun selectRealtimeHourly(dataParam: DataParam): List<BwData>

    /**
     * This method querys data list for specified zone.
     * order by meterId, extId, sampleTime (all ASC)
     *
     * @param map
     * @return
     */
    fun selectZoneRealtimeHourly(map: Map<String, Any>): List<BwData>

    /**
     * Almost same as {[.selectRealtimeHourly] but order by 'sampleTime' firstly
     * then meterId.
     *
     * @param dataParam
     * @return
     */
    fun selectDmaRealtimeHourly(dataParam: DataParam): List<BwData>

    /**
     * ORDER BY sample_time DESC.
     *
     * @param map
     * @return
     */
    fun selectRealtimeReverse(map: Map<String, Any>): List<BwData>

    fun statRealtime(map: Map<String, Any>): List<BwData>

    fun statRealtimeDma(dataParam: DataParam): List<BwData>

    fun selectDmaRealtime(dataParam: DataParam): List<BwData>

    fun insertRealtime(data: BwData): Int

    /**
     * 更新全部字段
     */
    fun updateRealtime(data: BwData): Int

    /**
     * 更新全部 none-null 字段
     */
    fun updateRealtimeByValue(data: BwData): Int

    /**
     * 只更新压力
     */
    fun updateRealtimePressure(data: BwData): Int

    /**
     * 只更新反向行度
     */
    fun updateRealtimeRevert(data: BwData): Int

    /**
     * 只更新正向行度
     */
    fun updateRealtimeForward(data: BwData): Int

    /**
     * Count the rows before deletion.
     *
     * @param map
     * @return
     */
    fun countDeleteRealtime(map: Map<String, Any>): Int?

    fun deleteRealtime(map: Map<String, Any>): Int

    /**
     * 删除单个数据点.
     * @param data
     * @return
     */
    fun deleteRealtimeSingle(data: BwData): Int

    /**
     * <pre>
     * <parameterMap type="map" id="realtimeMap">
     * <parameter property="meterId"></parameter>
     * <parameter property="sampleTime1"></parameter>
     * <parameter property="sampleTime2"></parameter>
     * <parameter property="dmaId"></parameter>
     * <parameter property="firmId"></parameter>
    </parameterMap> *
    </pre> *
     *
     * @param dataParam
     * @return - map: minDate, maxDate
     */
    fun realtimeDateRange(dataParam: DataParam): DataRange?

    /**
     * check realtime range for specified zone.
     *
     * @param map
     * @return
     */
    fun realtimeDateRangeZone(map: Map<String, Any>): List<DataRange>

    /**
     * Gets date range for specified DMA.
     *
     * @param dataParam - must contain key 'dmaId' but no 'meterId'.
     * @return - map: minDate, maxDate
     */
    fun realtimeDateRangeDma(dataParam: DataParam): DataRange

    /**
     * <pre>
     * <parameterMap type="map" id="rtuLogMap">
     * <parameter property="logTime1"></parameter>
     * <parameter property="logTime2"></parameter>
     * <parameter property="rtuId"></parameter>
     * <parameter property="meterId"></parameter>
     * <parameter property="index"></parameter>
     * <parameter property="rows"></parameter>
     * <parameter property="firmId"></parameter>
    </parameterMap> *
    </pre> *
     *
     * @param map
     * @return
     */
    fun selectRtuLog(map: Map<String, Any>): List<BwRtuLog>

    fun addRtuLog(rlog: BwRtuLog): Int

    fun deleteRtuLog(map: Map<String, Any>): Int

    fun selectRtuList(map: Map<String, Any>): List<BwRtu>

    fun addRtu(rtu: BwRtu): Int

    fun updateRtu(rtu: BwRtu): Int

    fun deleteRtu(map: Map<String, Any>): Int

    companion object {
        val METER_ID = "meterId"
        val SAMPLE_TIME1 = "sampleTime1"
        val SAMPLE_TIME2 = "sampleTime2"
    }
}
